package com.example.canalclientsync.utils;

import com.example.canalclientsync.canal.po.Dir;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
 * @author chen.cr
 * @since 2023/9/15
 */
public class CommonUtils {

    public static final int BATCH_SIZE = 1000;
    public static final int RETRY_LIMIT = 3;

    /**
     * 项目文件夹对应的dir type
     */
    public static final int PROJECT_DIR_TYPE = 404;
    /**
     * 项目目录文件夹
     */
    public static final int PROJECT_CATALOG_DIR_TYPE = 405;

    /**
     * 部门文件夹
     */
    public static final int DEPARTMENT_DIR_TYPE = 402;
    /**
     * 个人空间文件夹
     */
    public static final int PERSONAL_SPACE_DIR_TYPE = 6100;


    public static final String PROJECT_SPACE_DIR_TYPE_S = "/6104";
    public static final String PROJECT_DIR_TYPE_S = "/404";
    public static final String PROJECT_CATALOG_DIR_TYPE_S = "/405";
    public static final String PROJECT_INNER_DIR_TYPE_S = "/15";

    public static final String DEPARTMENT_SPACE_DIR_TYPE_S = "/6102";
    public static final String DEPARTMENT_DIR_TYPE_S = "/402";
    public static final String DEPARTMENT_INNER_DIR_TYPE_S = "/12";


    private static final String RLX = "_";

    /**
     * 根据文件夹，获取叶子部门
     *
     * @param dir
     * @return
     */
    public static Long getLeafDepartmentDirIdFormDir(Dir dir) {
        String treeType = dir.getTreeType()
                             .replaceAll(DEPARTMENT_SPACE_DIR_TYPE_S, RLX)
                             .replaceAll(DEPARTMENT_DIR_TYPE_S, RLX);
        int firstInnerDirIndex = treeType.indexOf(DEPARTMENT_INNER_DIR_TYPE_S);
        if (firstInnerDirIndex == -1) {
            firstInnerDirIndex = treeType.length();
        }

        String[] split = dir.getTreeId().replaceFirst("/", "").split("/");
        return Long.parseLong(split[firstInnerDirIndex - 1]);
    }


    /**
     * 根据文件夹，获取叶子部门
     *
     * @param dir
     * @return
     */
    public static Long getLeafProjectCatalogDirIdFormDir(Dir dir) {
        String treeType = dir.getTreeType()
                             .replaceAll(PROJECT_SPACE_DIR_TYPE_S, RLX)
                             .replaceAll(PROJECT_DIR_TYPE_S, RLX)
                             .replaceAll(PROJECT_CATALOG_DIR_TYPE_S, RLX);
        int firstInnerDirIndex = treeType.indexOf(PROJECT_INNER_DIR_TYPE_S);
        if (firstInnerDirIndex == -1) {
            firstInnerDirIndex = treeType.length();
        }

        String[] split = dir.getTreeId().replaceFirst("/", "").split("/");
        return Long.parseLong(split[firstInnerDirIndex - 1]);
    }


    //Object转Map
    public static Map<String, Object> beanToMap(Object obj) {
        Map<String, Object> map = new HashMap<>();
        Class<?> cla = obj.getClass();
        Field[] fields = cla.getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            String keyName = field.getName();
            Object value = null;
            try {
                value = field.get(obj);
            }
            catch (IllegalAccessException e) {
                //
            }
            if (value == null)
                value = "";
            map.put(keyName, value);
        }
        return map;
    }

}
